#!/usr/bin/env bash

function usage(){
cat << HELP
    dockertags  --  list all tags for a Docker image on a remote registry.

EXAMPLE:
    - list all tags for ubuntu:
       dockertags ubuntu

    - list all php tags containing apache:
       - 查看 php 带有 apache 的镜像
       dockertags php apache

       - 查看 php alpine 镜像
       dockertags php alpine

HELP
}


if [ $# -lt 1 ]; then
    usage
    exit
fi

image="$1"
tags=`wget -q https://registry.hub.docker.com/v1/repositories/${image}/tags -O -  \
| sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' \
| tr '}' '\n'  \
| awk -F: -v image=${image} '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'`

if [ -n "$2" ]; then
	tags=` echo "${tags}" | grep "$2" `
fi
echo "${tags}"